package com.monkeygroup.mygoplus.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * @Description:日期操作类
 * @Date 2014年8月15日
 * @Author
 * @Copyright
 */
public class FSDateTime {
  
	public static String getDateStr(Calendar c,String formatstr){
		 SimpleDateFormat format = new SimpleDateFormat(formatstr);
		 return format.format(c.getTime());
	}
	
	public static String getDateStr(String formatstr){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		 SimpleDateFormat format = new SimpleDateFormat(formatstr);
		 return format.format(c.getTime());
	}
	
	/**
	 * @return 当前日期格式 yyyy-mm-dd
	 */
	public static String getDate(){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		int myear=c.get(Calendar.YEAR);
		int mmonth=c.get(Calendar.MONTH)+1;
		int mday=c.get(Calendar.DAY_OF_MONTH);
		String re=String.format("%d-%02d-%02d",myear,mmonth,mday);
		return re;
	}
	
	
	public static String getDate(Calendar c){
		 
		int myear=c.get(Calendar.YEAR);
		int mmonth=c.get(Calendar.MONTH)+1;
		int mday=c.get(Calendar.DAY_OF_MONTH);
		String re=String.format("%d-%02d-%02d",myear,mmonth,mday);
		return re;
	}
	
	public static String getDate(long t){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		c.setTimeInMillis(t);
		int myear=c.get(Calendar.YEAR);
		int mmonth=c.get(Calendar.MONTH)+1;
		int mday=c.get(Calendar.DAY_OF_MONTH);
		String re=String.format("%d-%02d-%02d",myear,mmonth,mday);
		return re;
	}
 
	public static Calendar getCalendarByString(String str,String formatstr){
		try{
			SimpleDateFormat sdf= new SimpleDateFormat(formatstr);
			Date date =sdf.parse(str);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar;
		}catch(Exception e){
			return Calendar.getInstance();
		}
	}
	
	public static Calendar getCalendarByString2(String str){
		try{
			SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date date =sdf.parse(str);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar;
		}catch(Exception e){
			return Calendar.getInstance();
		}
	}
	
	
	/**
	 * @return 取得当前时间，精确到秒 hh:mm:ss
	 */
	public static String getTimeSec(){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		int mHOUR=c.get(Calendar.HOUR_OF_DAY);
		int mMINUTE=c.get(Calendar.MINUTE);
		int mSECOND=c.get(Calendar.SECOND);
		String re=String.format("%02d:%02d:%02d",mHOUR,mMINUTE,mSECOND);
		return re;
	}
	
	
	/**
	 * @return 取得当前时间，精确到分 hh:mm
	 */
	public static String getTime(){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		int mHOUR=c.get(Calendar.HOUR_OF_DAY);
		int mMINUTE=c.get(Calendar.MINUTE);
		String re=String.format("%02d:%02d",mHOUR,mMINUTE);
		return re;
	}
	
	
	   
	/**
	 * @return 当前日期短字符串 yyyymmddhhmmss
	 */
	public static String getDateTimeShort(){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		int myear=c.get(Calendar.YEAR);
		int mmonth=c.get(Calendar.MONTH)+1;
		int mday=c.get(Calendar.DAY_OF_MONTH);
		int hour=c.get(Calendar.HOUR_OF_DAY);
		int min=c.get(Calendar.MINUTE);
		int sec=c.get(Calendar.SECOND);
		String re=String.format("%d%02d%02d%02d%02d%02d",myear,mmonth,mday,hour,min,sec);
		return re;
	}
	
	/**
	 * @return 取得当前日期和时间 YYYY-MM-DD HH:MM
	 */
	public static String getDateTime(){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		int myear=c.get(Calendar.YEAR);
		int mmonth=c.get(Calendar.MONTH)+1;
		int mday=c.get(Calendar.DAY_OF_MONTH);
		int hour=c.get(Calendar.HOUR_OF_DAY);
		int min=c.get(Calendar.MINUTE);
		String re=String.format("%d-%02d-%02d %02d:%02d",myear,mmonth,mday,hour,min);
		return re;
	}
	
	public static String getDateTime(Calendar c){
		int myear=c.get(Calendar.YEAR);
		int mmonth=c.get(Calendar.MONTH)+1;
		int mday=c.get(Calendar.DAY_OF_MONTH);
		int hour=c.get(Calendar.HOUR_OF_DAY);
		int min=c.get(Calendar.MINUTE);
		String re=String.format("%d-%02d-%02d %02d:%02d",myear,mmonth,mday,hour,min);
		return re;
	}
	
	 
	
	
	/**
	 * @return 取得当前日期和时间 YYYY-MM-DD HH:MM:SS
	 */
	public static String getDateTimeSec(){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		int myear=c.get(Calendar.YEAR);
		int mmonth=c.get(Calendar.MONTH)+1;
		int mday=c.get(Calendar.DAY_OF_MONTH);
		int hour=c.get(Calendar.HOUR_OF_DAY);
		int min=c.get(Calendar.MINUTE);
		int sec=c.get(Calendar.SECOND);
		String re=String.format("%d-%02d-%02d %02d:%02d:%02d",myear,mmonth,mday,hour,min,sec);
		return re;
	}
	
	 
	
	/**
	 * @return 取得当前日期和时间 YYYY-MM-DD HH:MM
	 */
	public static String getDateTime(long t){
		Calendar c=Calendar.getInstance(Locale.CHINA);
		c.setTimeInMillis(t);
		int myear=c.get(Calendar.YEAR);
		int mmonth=c.get(Calendar.MONTH)+1;
		int mday=c.get(Calendar.DAY_OF_MONTH);
		int hour=c.get(Calendar.HOUR_OF_DAY);
		int min=c.get(Calendar.MINUTE);
		String re=String.format("%d-%02d-%02d %02d:%02d",myear,mmonth,mday,hour,min);
		return re;
	}
	
	
	
 
}
